package com.ykdl.member.kid.doctor;

import java.util.ArrayList;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.BitmapUtils;
import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.DoctorsBean;
import com.ykdl.member.kid.beans.DoctorsListBean;
import com.ykdl.member.kid.beans.Memberbean;
import com.ykdl.member.kid.beans.Memberlistbean;
import com.ykdl.member.kid.beans.UserActorBean;
import com.ykdl.member.kid.circle.MemberListActivity;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.image.ImageLoader;
import com.ykdl.member.kid.image.ImageOptionsFactory;
import com.ykdl.member.kid.widget.AsyncImageView;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/**
 * 医生列表Acivity
 * @author chenshuai
 */
public class DoctorListAcivity extends BaseScreen{
	private RefreshListView doctor_list ;
	private DotorListAdatapr adapter ;
	private ArrayList<UserActorBean>  doctorlist;
	private String TAG = "DoctorListAcivity";
	private BitmapUtils bitmapUtil ; 
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("专家", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, 0, 0, null, R.drawable.title_bg, false);
		setContent(R.layout.doctorlistacivity_xml);
		bitmapUtil = new BitmapUtils(this);
		bitmapUtil.configDefaultLoadingImage(R.drawable.defualt_doctor_icon);
		bitmapUtil.configDefaultLoadFailedImage(R.drawable.defualt_doctor_icon);
		inint();
	}
	public void inint() {
		adapter = new DotorListAdatapr();
		doctor_list = (RefreshListView)findViewById(R.id.doctor_list);
		doctor_list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				UserActorBean bean = (UserActorBean)adapter.getItem(arg2);
				Intent intent = new Intent(DoctorListAcivity.this,DoctorInfoActivity.class);
				intent.putExtra("doctor_id", bean.getActor_id()+"");
				intent.putExtra("doctor_name",bean.getDisplay_name());
				DoctorListAcivity.this.startActivity(intent);
			}
		});
		doctor_list.setMoreButtoIsGon(true);
		inintdata() ;
	}
	public void inintdata() {
		showProgress("正在加载中,请稍等...");
		String  pageurl =KidConfig.GET_DOCTOR_LIST;
		HttpBaseRequest request  =  Wxxr.getInstance().getBaseRequest(pageurl);
		 TaskManager.startHttpRequest(request, new getDoctorListTag(),  DoctorsListBean.class);
	}
	
	class getDoctorListTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			finishProgress();
			if (t instanceof DoctorsListBean) {
				DoctorsListBean bean = (DoctorsListBean)t;
				adapter.setList(bean.getList());
				doctor_list.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				Toast.makeText(mContext, "成功", 1).show();
			}
		
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "加载失败", 1).show();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().setCurrentActivity(TAG);
		super.onResume();
	}
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		ImageLoader.getInstance().pause(TAG);
		super.onPause();
	}
	class DotorListAdatapr extends BaseAdapter {
		
		private ArrayList<UserActorBean>   list;
		
		public void setList(ArrayList<UserActorBean> list) {
			this.list = list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			if (list==null) {
				return 0;
			}else {
				return list.size();
			}
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position - 1);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = RelativeLayout.inflate(mContext,
						R.layout.doctor_list_item, null);
				 convertView.setTag(holder);
				 holder.head = (ImageView)convertView.findViewById(R.id.iask_evetn_dochead);
				 holder.doctor_name = (TextView)convertView.findViewById(R.id.doctor_name);
				 holder.doctor_position = (TextView)convertView.findViewById(R.id.doctor_position);
				 holder.doctor_info = (TextView)convertView.findViewById(R.id.doctor_info);
				}else {
					holder = (ViewHolder) convertView.getTag();
				}
			if (list.get(position).getAvatar_file_meta()!=null) {
				String url = list.get(position).getAvatar_file_meta().getDownload_urls().getSmal().getUrl();
				bitmapUtil.display(holder.head, url);
			}else {
				holder.head.setImageResource(R.drawable.defualt_doctor_icon);
			}
			holder.doctor_name.setText(list.get(position).getDisplay_name()+"");
			if (list.get(position).getProfile()!=null) {
				holder.doctor_position.setText(list.get(position).getProfile().getTitle()+"");
				holder.doctor_info.setText(list.get(position).getProfile().getInstruction()+"");
			}else {
				holder.doctor_position.setText("");
				holder.doctor_info.setText("");
			}
			final int doctor_id = list.get(position).getDoctor_id() ;
			 holder.head.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
				}
			});
			return convertView;
		}
		class ViewHolder {
			ImageView head;
			TextView doctor_name;
			TextView doctor_position;
			TextView doctor_info;
			
		}
	}
}
